package br.edu.sistemaacademico;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.facade.DataFacade;
import br.edu.sistemaacademico.pojo.Classe;
import br.edu.sistemaacademico.pojo.Disciplina;
import br.edu.sistemaacademico.pojo.Professor;
import br.edu.sistemaacademico.pojo.Turma;
import br.edu.sistemaacademico.state.StateHandler;
import client.ClientCommand;

/**
 *
 * @author baron
 */
public class frmProfessor extends FrmModelo{
	Classe classe;
	StateHandler stateAlterarExcuir = new StateHandler(false);
	Professor professor;
	Turma turma;
	public List<Object> campos = new ArrayList<Object>();
    private List<Object> valores = new ArrayList<Object>();
    private List<Object> tamanho = new ArrayList<Object>();
    List<Turma> extra;
    TabelaModelo tabModel;
     
    public frmProfessor(java.awt.Frame parent, boolean modal, Professor professor) {
        super(parent, modal);
        this.professor = professor;
        campos.add("codigo");
        campos.add("nome");
        campos.add("periodo");
        campos.add("tamanho");
        tamanho.add("70");
        tabModel = new TabelaModelo(campos, tamanho, getJTable());
        tabModel.busca(getTfBuscar());
        ListSelectionModel listModel = getJTable().getSelectionModel();
        listModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        listModel.addListSelectionListener(list);
        
        setTitle("Professor");
        getbtnAlterar().setActionCommand("alterar");
        getbtnAlterar().addActionListener(listener);
        getbtnVoltar().addActionListener(listener);
        getbtnVoltar().setActionCommand("voltar");
        getJbtnGravar().setActionCommand("fechar nota");
        getJbtnGravar().addActionListener(listener);
        getbtnExcluir().setActionCommand("excluir");
        getbtnExcluir().addActionListener(listener);
        setButtons();
        setStateAlteraExcuir();
        atualiza();
        setVisible(true);
    }

        public void setButtons(){
        JButton btn1 = getJbtnGravar();
        JButton btn2 = getbtnAlterar();
        JButton btn3 = getbtnExcluir();
        JButton btn4 = getbtn4();
        JButton btn5 = getbtn5();
        JButton btn6 = getbtn6();

        btn1.setText("Fechar nota");
        btn2.setText("alterar");
        btn3.setText("Relatorio");
        btn4.setVisible(false);
        btn5.setVisible(false);
        btn6.setVisible(false);
    }
        public void atualiza(){
        	((DefaultTableModel) getJTable().getModel()).setNumRows(0);
			getJTable().updateUI();
        	this.extra = (List) ClientCommand.carregar("TurmaService","professor ="+professor.getId(), "");
    		DataFacade entrada = new DataFacade();   
        	for(int i = 0; i < extra.size();i++ ){
        		valores = new LinkedList<Object>();
        		valores.add(extra.get(i).getId());
        		/*try {
					extra.get(i).setClasse((Classe)classeDao.getObject(" codigo="+extra.get(i).getClasse(), "*", ""));
	        		extra.get(i).getClasse().setDisicplina( (Disciplina)  disciplinaDao.getObject("codigo= "+extra.get(i).getClasse().getDisicplina().getId(), "*", ""));
        		} catch (SQLException e) {
					e.printStackTrace();
				}*/
        		valores.add(extra.get(i).getClasse().getDisicplina().getNome());
        		valores.add(extra.get(i).getPeriodo());
        		valores.add(extra.get(i).getTamanho());
        		tabModel.setValores(valores);
        		valores.clear();
        		valores.removeAll(extra);
        	}
        }
        ActionListener listener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getActionCommand().equals("voltar"))
					dispose();
				if(e.getActionCommand().equals("fechar nota"))
					new frmFinalizaTurma(turma);
				else if(e.getActionCommand().equals("alterar"))
					new frmAtualizarProfessor(professor);
				atualiza();
				
			}
		};
		ListSelectionListener list = new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				try{
				turma = getSelectedTurma(Long.parseLong(getJTable().getValueAt(getJTable().getSelectedRow(), 0).toString()));
				stateAlterarExcuir.change(true);
				}catch(ArrayIndexOutOfBoundsException ea){
					turma = null;
					stateAlterarExcuir.change(false);
				}	
			}
		};
		
		public Turma getSelectedTurma(Long codigo){
			return (Turma) ClientCommand.carregaObjeto("TurmaService", "codigo ="+codigo, "");
		}
		public void remover(){
			ClientCommand.deletar("TurmaService",turma);
			atualiza();
		}
	    public void setStateAlteraExcuir(){
	    	//stateAlterarExcuir.addComponent(getbtnAlterar());
	    	stateAlterarExcuir.addComponent(getbtnExcluir());
	    	stateAlterarExcuir.addComponent(getbtn4());
	    	stateAlterarExcuir.addComponent(getbtn5());
	    }
		
}
